//
//    Tokaplot
//    https://code.google.com/p/tokaplot/
//
//    License: GPL v3
//    https://code.google.com/p/tokaplot/wiki/License
//
//    This file is part of Tokaplot.
//
//    Tokaplot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Tokaplot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Tokaplot.  If not, see <http://www.gnu.org/licenses/>
//

package com.tokaplot;

import android.app.Activity;
import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

//import android.util.Log;

public class ConfigGraphics extends Activity {
	
	private WidgetInfo wInfo = null;
	private AlertDialog dPlot_Grid = null;
	
	//private static final String TAG = "Tokaplot";
	
	private final static CharSequence[] gridItems = {"None", "Fine", "Coarse, no text", "Coarse, text", "Text only"};
    	
    public ConfigGraphics() {
        super();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setResult(RESULT_CANCELED);

        // Find widget id
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
        	wInfo = new WidgetInfo(intent);
        }
        if (wInfo.getID() == AppWidgetManager.INVALID_APPWIDGET_ID)
            finish();
        
        // -----------------------------------------------
    	//  Create dialogs
        // -----------------------------------------------
        AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(this);
        
		dPlot_Grid = builder.create();
		builder = new AlertDialog.Builder(this);
		builder.setTitle("Select grid layout");
		
		int checkedItem = 0;
		int gridType = wInfo.getGridType();
		if (gridType == 0) {
			checkedItem = 0; // No grid
		} else if (gridType == 1) {
			checkedItem = 1; // Fine grid 
		} else if (gridType == 2) {
			checkedItem = 2; // Coarse grid 
		} else if (gridType == 6) {
			checkedItem = 3; // Coarse grid with text
		} else if (gridType == 4) {
			checkedItem = 4; // Text only
		}
		
		builder.setSingleChoiceItems(gridItems, checkedItem, new DialogInterface.OnClickListener() {
	    	public void onClick(DialogInterface dialog, int item) {
	    		if (item == 0) {
	    			wInfo.setGridType(0); // No grid
	    		} else if (item == 1) {
	    			wInfo.setGridType(1); // Fine grid 
	    		} else if (item == 2) {
	    			wInfo.setGridType(2); // Coarse grid 
	    		} else if (item == 3) {
	    			wInfo.setGridType(6); // Coarse grid with text
	    		} else if (item == 4) {
	    			wInfo.setGridType(4); // Text only
	    		}
	    		dPlot_Grid.dismiss();
	    	}
		});
		dPlot_Grid = builder.create();	
		
        // Setup view
        setContentView(R.layout.config_graphics);
        
        ((Button)findViewById(R.id.gconfig_button_plot_grid_col)).getBackground().setColorFilter(wInfo.getGridCol(),PorterDuff.Mode.MULTIPLY);
        findViewById(R.id.gconfig_button_plot_grid_col).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new DialogColour(ConfigGraphics.this, wInfo.getGridCol(), new DialogColour.OnColourChangedListener() {
					
					public void colourChanged(int colour) {
						wInfo.setGridCol(colour);
						((Button)findViewById(R.id.gconfig_button_plot_grid_col)).getBackground().setColorFilter(wInfo.getGridCol(),PorterDuff.Mode.MULTIPLY);
					}
				}).show();
            }
        });
        
//        findViewById(R.id.gconfig_button_plot_grid_sty).setOnClickListener(new View.OnClickListener() {
//            public void onClick(View v) {
//                
//            	LinePickerDialog dLinePicker = new LinePickerDialog(ConfigGraphics.this);
//            	dLinePicker.setup(wInfo.getGridCol(), wInfo.getGridStyle(), new LinePickerDialog.OnLineChangedListener() {
//					
//					public void lineChanged(int style) {
//						wInfo.setGridStyle(style);
//					}
//				});
//            	dLinePicker.show();
//            }
//        });
        
        ((Button)findViewById(R.id.gconfig_button_plot_text_col)).getBackground().setColorFilter(wInfo.getTextCol(),PorterDuff.Mode.MULTIPLY);
        findViewById(R.id.gconfig_button_plot_text_col).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new DialogColour(ConfigGraphics.this, wInfo.getTextCol(), new DialogColour.OnColourChangedListener() {
					
					public void colourChanged(int colour) {
						wInfo.setTextCol(colour);
						((Button)findViewById(R.id.gconfig_button_plot_text_col)).getBackground().setColorFilter(wInfo.getTextCol(),PorterDuff.Mode.MULTIPLY);
					}
				}).show();
            }
        });
        
        findViewById(R.id.gconfig_button_plot_grid_layout).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	dPlot_Grid.show();
            }
        });
        
        findViewById(R.id.gconfig_button_plot_background).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new DialogShading(ConfigGraphics.this, wInfo.getBackCol1(), wInfo.getBackCol2(), new DialogShading.OnColourChangedListener() {
					
					public void colourChanged(int colour1, int colour2) {
						wInfo.setBackCol1(colour1);
						wInfo.setBackCol2(colour2);
					}
				}).show();
            }
        });
        
        if (wInfo.getShowMaxMin()) {
        	((CheckBox)findViewById(R.id.gconfig_check_maxmin)).setChecked(true);
        } else {
        	((CheckBox)findViewById(R.id.gconfig_check_maxmin)).setChecked(false);
        }
        if (wInfo.getUseLocalRange()) {
        	((CheckBox)findViewById(R.id.gconfig_check_range)).setChecked(true);
        } else {
        	((CheckBox)findViewById(R.id.gconfig_check_range)).setChecked(false);
        }
		
        // -----------------------------------------------
        //  Set listeners
        // -----------------------------------------------       
        findViewById(R.id.gconfig_button_copy).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	wInfo.copyPreferences(getApplicationContext(), wInfo.getIDLast());
    	        
                // Pass back to widget
                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, wInfo.getID());
                setResult(RESULT_OK, resultValue);
                
    	        // Return options
                Intent widgetIntent = new Intent();
                widgetIntent.setAction(TokaplotService.ACTION_APPWIDGET_CONFIG);
                
    	    	wInfo.setIntentWithInfo(widgetIntent);
    	    	sendBroadcast(widgetIntent);
    	    	
                finish();
            }
        });
        
        findViewById(R.id.gconfig_button_done).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
    	    	// Get checks
    	        if (((CheckBox)findViewById(R.id.gconfig_check_maxmin)).isChecked() == true)
    	        	wInfo.setShowMaxMin();
    	        else
    	        	wInfo.unsetShowMaxMin();
    	        if (((CheckBox)findViewById(R.id.gconfig_check_range)).isChecked() == true)
    	        	wInfo.setUseLocalRange();
    	        else
    	        	wInfo.unsetUseLocalRange();
    	        
                // Pass back to widget
                Intent resultValue = new Intent();
                resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, wInfo.getID());
                setResult(RESULT_OK, resultValue);
                
    	        // Return options
                Intent widgetIntent = new Intent();
                widgetIntent.setAction(TokaplotService.ACTION_APPWIDGET_CONFIG);
                
    	    	wInfo.setIntentWithInfo(widgetIntent);
    	    	sendBroadcast(widgetIntent);
    	    	
                finish();
            }
        });
        
        findViewById(R.id.gconfig_button_cancel).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
                // Pass back to widget
            	setResult(RESULT_CANCELED);    	    	
                finish();
            }
        });
    }
}
